package bgpemulation;

import java.io.Serializable;
import java.util.Arrays;

public class ASPath implements Comparable<ASPath>, Serializable{
	

	private static final long serialVersionUID = 1L;
	public static final String ASN_SEP = " ";
	private int[] path = null;
	
	public ASPath(String path){
		String[] asns = path.split(ASN_SEP);
		this.path = new int[asns.length];
		for(int i = 0; i<asns.length; i++){
			this.path[i] = Integer.parseInt(asns[i]);
		}
	}
	
	
	
	@Override
	public boolean equals(Object o){
		if(!(o instanceof ASPath)){
			return false;
		}
		return compareTo((ASPath)o)==0;
	}

	@Override
	public int compareTo(ASPath other) {
		int diff = path.length - other.path.length;
		if(diff!=0){
			return diff;
		}
		for(int i = 0; i<path.length; i++){
			diff = path[i] - other.path[i];
			if(diff!=0){
				return diff;
			}
		}
		return 0;
	}
	
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		for(int i : path){
			sb.append(i);
			sb.append(" ");
		}
		return sb.toString().trim();
	}
	
	public void print(){
		System.out.println(toString());
	}
	
	public int[] getArray(){
		return Arrays.copyOf(path, path.length);
	}
	
	public int getLength(){
		return path.length;
	}
	
	public int getHead(){
		return path[0];
	}
	
	public int getTail(){
		return path[path.length - 1];
	}
}